<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Example 7.4: Binary hypothesis testing</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch07_statistical_estim/html/detector2.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Example 7.4: Binary hypothesis testing</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Figure 7.4</span>
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original version by Lieven Vandenberghe</span>
<span class="comment">% Updated for CVX by Michael Grant, 2005-12-19</span>

<span class="comment">% Generate the data</span>
P = [0.70  0.10
     0.20  0.10
     0.05  0.70
     0.05  0.10];
[n,m] = size(P);

<span class="comment">% Construct the tradeoff curve by finding the</span>
<span class="comment">% the Pareto optimal deterministic detectors,</span>
<span class="comment">% which are the curve's vertices</span>

nopts   = 1000;
weights = logspace(-5,5,nopts);
obj     = [0;1];
inds    = ones(n,1);

<span class="comment">% minimize  -t1'*q1 - w*t2'*q2</span>
<span class="comment">% s.t.      t1+t2 = 1,  t1,t2 \geq 0</span>

next = 2;
<span class="keyword">for</span> i = 1 : nopts,
   PW = P * diag( [ 1 ; weights(i) ] );
   [ maxvals, maxinds ] = max( PW' );  <span class="comment">% max elt in each row</span>
   <span class="keyword">if</span> (~isequal(maxinds', inds(:,next-1)))
       inds(:,next) = maxinds';
       T = zeros(m,n);
       <span class="keyword">for</span> j=1:n
          T(maxinds(1,j),j) = 1;
       <span class="keyword">end</span>;
       obj(:,next) = 1-diag(T*P);
       next = next+1;
   <span class="keyword">end</span>;
<span class="keyword">end</span>;
plot(obj(1,:), obj(2,:),[0 1], [0 1],<span class="string">'--'</span>);
grid <span class="string">on</span>
<span class="keyword">for</span> i=2:size(obj,2)-1
   text(obj(1,i),obj(2,i),[<span class="string">'a'</span>, num2str(i-1)]);
<span class="keyword">end</span>;

<span class="comment">% Minimax detector: not deterministic</span>

cvx_begin
    variables <span class="string">T( m, n )</span> <span class="string">D( m, m )</span>
    minimize <span class="string">max( D(1,2), D(2,1) )</span>
    subject <span class="string">to</span>
        D == T * P;
        sum( T, 1 ) == 1;
        T &gt;= 0;
cvx_end

objmp = 1 - diag( D );
text( objmp(1), objmp(2), <span class="string">'b'</span> );
xlabel(<span class="string">'P_{fp}'</span>); ylabel(<span class="string">'P_{fn}'</span>);

<span class="comment">%print -deps roc.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling sedumi: 10 variables, 5 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 5, order n = 11, dim = 11, blocks = 1
nnz(A) = 18 + 0, nnz(ADA) = 13, nnz(L) = 9
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            3.84E+00 0.000
  1 :   4.94E-01 1.11E+00 0.000 0.2891 0.9000 0.9000   3.24  1  1  7.1E-01
  2 :   2.26E-01 2.18E-01 0.000 0.1960 0.9000 0.9000   1.25  1  1  1.7E-01
  3 :   1.82E-01 5.12E-02 0.000 0.2353 0.9000 0.9000   1.05  1  1  4.6E-02
  4 :   1.68E-01 4.07E-03 0.000 0.0794 0.9900 0.9900   1.01  1  1  4.0E-03
  5 :   1.67E-01 1.65E-06 0.000 0.0004 0.9999 0.9999   1.00  1  1  
iter seconds digits       c*x               b*y
  5      0.0   Inf  1.6666666667e-01  1.6666666667e-01
|Ax-b| =   1.6e-16, [Ay-c]_+ =   0.0E+00, |x|=  1.9e+00, |y|=  3.5e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    2.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.166667
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="detector2__01.png" alt=""> 
</div>
</div>
</body>
</html>